﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;

namespace CommonLibrary
{
    /// <summary>
    /// rsa 加密帮助类
    /// </summary>
    public class EncryptDecryptHelper
    {
        #region 利用MD5对字符串进行加密
        /// <summary>
        /// 秘钥
        /// </summary>
        public static string KEY_64 = "LoveApps";// "VavicApp";//密码只能8个字符
        /// <summary>
        /// 偏移量
        /// </summary>
        public static string IV_64 = "ThisIsIV";// "VavicApp";//向量只能8个字符
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string EncryptMD5(string data)
        {
            try
            {
                byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
                byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                int i = cryptoProvider.KeySize;
                MemoryStream ms = new MemoryStream();
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cst);
                sw.Write(data);
                sw.Flush();
                cst.FlushFinalBlock();
                sw.Flush();
                return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            }
            catch (Exception x)
            {
                return x.Message;
            }
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string DecryptMD5(string data)
        {
            try
            {
                byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
                byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
                byte[] byEnc;
                byEnc = Convert.FromBase64String(data); //把需要解密的字符串转为8位无符号数组
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream(byEnc);
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cst);
                return sr.ReadToEnd();
            }
            catch (Exception x)
            {
                HWMessageBox.Show(null, x.Message, "转换出错");
                return x.Message;
            }
        }
        //解密

        #endregion 利用MD5对字符串进行加密

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public static string Encrypt(string plainText)
        {
            string result = "";
            try
            {
                string md5 = EncryptMD5(plainText);
                result = $"T{new string(md5.ToCharArray().Reverse().ToArray())}M";
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string Decrypt(string message)
        {
            string result = "";
            try
            {
                if (!string.IsNullOrWhiteSpace(message) && message.Length > 2)
                {
                    message = new string(message.Substring(1, message.Length - 2).ToCharArray().Reverse().ToArray());
                    result = DecryptMD5(message);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
    }
}
